package com.etrans.web.gps.business.log.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.log.domain.LoginRecordVO;
import com.etrans.web.gps.business.log.service.LoginRecordService;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;


/**
 * 查看登录日志controller
 * @author zhangw
 *
 */
@Controller
@RequestMapping("/log/loginRecord/*")
public class LoginRecordController extends BaseController{
	private final static Logger logger = LoggerFactory.getLogger(LoginRecordController.class);
	
	@Autowired
	private LoginRecordService loginRecordService;
	
	/**
	 *用户登录日志 
	 * @Description 页面跳转
	 */
	@RequestMapping("getLoginRecord.html")
	public String getLoginRecord(){
		return "views/business/log/loginRecord";
	}
	
	/**
	 *  获取用户登录日志 
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "getLoginRecordList.html",method = RequestMethod.POST)
	public PageVo getLoginRecordList(String paramsGrid, HttpServletRequest request){
		SysUser user = getSysUser();
		Map<String, Object> param = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		param.put("userId", user.getId());
		PageVo pageVo = loginRecordService.getLoginRecordList(param);
        return  pageVo;
	}

	/**
	 *
	 *@description  导出登录日志数据
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportLoginRecord.html")
	public void exportLoginRecord( HttpServletRequest request, HttpServletResponse response, String queryParams, 
			String title, String StartPage, String EndPage, String PageSize,String SortName,String colModel) {
		//获取参数
		HashMap<String, Object> params = new HashMap<String, Object>();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		try {
			Map<String, Object> paramMap = getReqParameters();
			SysUser user = getSysUser();
			List<String> whereParamsList = JSON.parseObject(paramMap.get("queryParams").toString(), List.class);
			for (Object object : whereParamsList) {
				JSONObject jsonObject = (JSONObject) object;
				params.put(jsonObject.get("name").toString(),jsonObject.get("value"));
			}
			params.put("userId", user.getId());
			//分页条件
			int startPage = Integer.parseInt(StartPage);
			int endPage = Integer.parseInt(EndPage);
			int pageSize = Integer.parseInt(PageSize);
			params.put("page", startPage);
			params.put("pageSize", (endPage - startPage+1)*pageSize);		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取参数失败",e);
		}		
		//传入条件从MongoDB中查询出数据
		PageVo pageVo = loginRecordService.getLoginRecordList(params);	
		List<LoginRecordVO> list = (List<LoginRecordVO>)pageVo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i = 0; i < list.size(); i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			LoginRecordVO obj=(LoginRecordVO)list.get(i);
			map.put("userId", obj.getUserId()==null ? "" : obj.getUserId());
			map.put("userName", obj.getUser_name()==null ? "" : obj.getUser_name());
			map.put("loginName", obj.getLogin_name()==null ? "" : obj.getLogin_name());
			map.put("userRole", obj.getRole_name()==null ? "" : obj.getRole_name());
			map.put("loginIP", obj.getLoginIP()==null ? "" : obj.getLoginIP());
			map.put("loginAddress", obj.getLoginAddress()==null ? "" : obj.getLoginAddress());
			map.put("isSuccess", obj.getIsSuccess()==1 ? "成功" : "失败");
			map.put("loginTime", obj.getLoginTime()==null ? "" : sdf.format(obj.getLoginTime()));
			map.put("userPrecinct", obj.getArea_name()==null ? "" : obj.getArea_name());

			rows.add(map);
		}
		int index=-1;
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
}
